<template>
    <div class="chooser-component">
      <ul class="chooser-list">
        <li
          v-for="(item, index) in choooseList"
          :key="index"
          :title="item.label"
          @click="toggle(index)"
          :class="{active: checkActive(index)}"

        >
        {{ item.label }}
        </li>
      </ul>
    </div>
</template>

<script>
import _ from 'lodash'
export default {
  props: {
    choooseList: {
      type: Array,
      default: [{
        lable: 'test',
        index: 0
      }]
    }
  },
  data() {
    return {
      nowIndex: [0] // nowIndex 是一个数组，因为可以多选
    }
  },
  methods: {
    checkActive(index){
      // 在nowIndex 里查找是否有这一项，有则高亮显示
      return this.nowIndex.indexOf(index) !== -1
    },
    toggle(index) {
      if(this.nowIndex.indexOf(index) === -1) {
        return this.nowIndex.push(index)
      } else {
        this.nowIndex = _.remove(this.nowIndex, (ids) => {
          return ids !== index
        })
      }
      // 将多选框的值传出去
      let nowObjArray = _.map(this.nowIndex, (ids) => {
        return this.choooseList[ids]
      })
      this.$emit('on-change', nowObjArray)
    }
  }
}
</script>

<style scoped>
.chooser-component {
  position: relative;
  display: inline-block;
}
.chooser-list li{
  display: inline-block;
  border: 1px solid #e3e3e3;
  height: 25px;
  line-height: 25px;
  padding: 0 8px;
  margin-right: 5px;
  border-radius: 3px;
  text-align: center;
  cursor: pointer;
}
.chooser-list li.active {
  border-color: #4fc08d;
  background: #4fc08d;
  color: #fff;
}
</style>
